import { Component, OnInit ,ElementRef} from '@angular/core';
import { Router, NavigationExtras} from '@angular/router';
import {HouseServiceSvr} from '../../services/houseServiceSvr'
import { HouseList } from 'src/app/domains/houselist.domain';
import {CommonServiceSvr} from '../../service/common.service'
import {ActivatedRoute} from '@angular/router'

@Component({
  selector: 'app-house',
  templateUrl: './house.component.html',
  styleUrls: ['./house.component.less']
})
export class HouseComponent implements OnInit {

  // 除区域以外的房屋信息
  siftList:any=[
    {
      title:'分类',
      options:['合租','整租','不限']
    },
    {
      title:'面积',
      options:['50平以下','50-100平','100-150平','150-200平','200平以上','不限']
    },
    {
      title:'价格',
      options:['1000元以下','1000-1500元','1500-2000元','2000-2500元','2500-3000元','3000-3500元','4000元以上','不限']
    },
    {
      title:'顺序',
      options:['升序','降序']
    },
  ]

  
  goSearsh(){
    // console.log('SearchMsg');
    // console.log(this.SearchMsg);
    // this.houseServiceSvr.listHouse('',this.SearchMsg,'','',0,2000,0,20000,2,1,'',this.PageIndex,this.PageSize).then((res=>{
    //   // console.log(res);
    //   this.houseList=res._Items
    //   console.log(this.houseList);
    //   // console.log(this.houseList[0].Rent);
    // }))
    this.invokeListHouse(this._selectedProvince,this.selectedCity,this.selectedRegion,this.minSize,this.maxSize,this.minRent,this.maxRent,this.order,this.type,this.SearchMsg,this.PageIndex,this.PageSize)
  }

  // 房屋
  houseList:any

  // 区域
  _selectedProvince = '';
  selectedCity = '';
  selectedRegion='';
  provinceData = ['四川省'];
  cityData: { [place: string]: any[] } = {
  };
  RegionData:{[palce:string]:any[]}={
  }

  provinceChange(value: string): void {
    this.selectedCity = this.cityData[value][0];
    this.invokeListHouse(this._selectedProvince,this.selectedCity,this.selectedRegion,this.minSize,this.maxSize,this.minRent,this.maxRent,this.order,this.type,this.SearchMsg,this.PageIndex,this.PageSize)
  }

  cityChange(value: string): void {
    this.selectedRegion = this.RegionData[value][0];
    this.invokeListHouse(this._selectedProvince,this.selectedCity,this.selectedRegion,this.minSize,this.maxSize,this.minRent,this.maxRent,this.order,this.type,this.SearchMsg,this.PageIndex,this.PageSize)
  }

  reginChange(value: string): void {
    this.invokeListHouse(this._selectedProvince,this.selectedCity,this.selectedRegion,this.minSize,this.maxSize,this.minRent,this.maxRent,this.order,this.type,this.SearchMsg,this.PageIndex,this.PageSize)
  }

  constructor(
    private router:Router,
    private houseServiceSvr:HouseServiceSvr,
    private el:ElementRef,
    private commonServiceSvr:CommonServiceSvr,
    private route:ActivatedRoute
  ) {
    this.route.queryParams.subscribe((data) => {
      // data是一个对象，里面有传过来的值
         this.SearchMsg = data['SearchMsg'];
      });
   }

cityres:any=[]
reginres:any=[]
// 搜索信息
SearchMsg:string=''
  ngOnInit(): void {
    // this.type=0
    // 房屋信息接口
    this.SearchMsg=''
    console.log('this.SearchMsg:'+this.SearchMsg);
    this.houseServiceSvr.listHouse('',this._selectedProvince,this.selectedCity,this.selectedRegion,0,20000,0,20000,0,this.type,this.SearchMsg,this.PageIndex,this.PageSize).then((res=>{
      // console.log(res);
      console.log("Index:"+this.PageIndex);
      console.log("PageSize:"+this.PageSize);
      console.log("order"+this.order);
      
      this.houseList=res._Items
      // console.log('1');
      this.Total=res.MaxID
      console.log(res);
      // console.log(this.Total);
      // console.log(this.houseList[0].Rent);
    }))
    // 省市区结构接口
    this.commonServiceSvr.getAreaTree('').then((res)=>{
      // console.log('省市区结构：');
      // console.log(res._Items[4].AreaList._Items);
      // console.log(res);
      for(let k of res._Items){
        this.provinceData.push(k.Name)
        this.cityres=[]
        for(let dev of k.AreaList._Items){
          this.cityres.push(dev.Name)
          this.reginres=[]
          for(let key of dev.AreaList._Items){
            this.reginres.push(key.Name)
          }
        this.RegionData[dev.Name]=this.reginres
        }
        this.cityData[k.Name]=this.cityres
      }
      // console.log('provinceData:'+this.provinceData);
      // console.log('cityData:'+this.cityData);
    })
  }
  // 跳转到房屋详情页面
  gohouseDetail(e:number){
    let navigationExtras:NavigationExtras = {
      queryParams:{'key':e}
    }
    this.router.navigate(['/houseDetails'],navigationExtras)
  }
  invokeListHouse(province:string,city:string,region:string,minarea:number,maxarea:number,minrent:number,maxrent:number,order:number,type:number,SearchMsg:string,skip:number,size:number){
    this.houseServiceSvr.listHouse('',province,city,region,minarea,maxarea,minrent,maxrent,order,type,SearchMsg,skip,size).then((res=>{
      this.houseList=res._Items
      console.log(res);
      
      // this.Total=this.houseList.length
      this.Total=res.MaxID
      // console.log('点击');
      // console.log(this.houseList);
    }))
  }

// 面积上限
maxSize:number=0
// 面积下限
minSize:number=0
// 租金上限
maxRent:number=0
// 租金下限
minRent:number=0
// 类型
type:number=0
// 顺序
order:number=0
// skip
// skip:number=0
// size
// size:number=0

typeTure:string=''
RentTure:string=''
SizeTure:string=''
oderTure:string=''
areaTure:string=''

// 筛选
siftProvince(){
this.invokeListHouse(this._selectedProvince,this.selectedCity,this.selectedRegion,this.minSize,this.maxSize,this.minRent,this.maxRent,this.order,this.type,this.SearchMsg,this.PageIndex,this.PageSize)
// console.log('11:'+this._selectedProvince);
}

siftType(e:any){
  this.typeTure=e
if(e=='合租'){
  console.log('Type:'+2);
  this.type=2
}else if(e=='整租'){
  console.log('Type:'+1);
  this.type=1
}else{
  this.type=0
  console.log('Type:'+this.type);
}
this.invokeListHouse(this._selectedProvince,this.selectedCity,this.selectedRegion,this.minSize,this.maxSize,this.minRent,this.maxRent,this.order,this.type,this.SearchMsg,this.PageIndex,this.PageSize)
// this.el.nativeElement.querySelector('.reserve').innerHTML = '预约看房';
}
siftArea(e:any){
  this.areaTure=e
this.minSize=Number(e.substring(0,e.indexOf('-')))
this.maxSize=Number(e.substring(e.indexOf('-')+1,e.indexOf('平')))
if(e=='200平以上'){
  this.minSize=200
  this.maxSize=10000
}else if(e=='50平以下'){
  this.minSize=1
  this.maxSize=50
}else if(e=='不限'){
  this.minSize=1
  this.maxSize=10000
}
  // console.log('area'+this.minSize);
  // console.log('area'+this.maxSize);
this.invokeListHouse(this._selectedProvince,this.selectedCity,this.selectedRegion,this.minSize,this.maxSize,this.minRent,this.maxRent,this.order,this.type,this.SearchMsg,this.PageIndex,this.PageSize)

}
siftRent(e:any){
  this.RentTure=e
  this.minRent=Number(e.substring(0,e.indexOf('-')))
this.maxRent=Number(e.substring(e.indexOf('-')+1,e.indexOf('元')))
if(e=='4000元以上'){
  this.minRent=4000
  this.maxRent=10000
}else if(e=='1000元以下'){
  this.minRent=1
  this.maxRent=1000
}else if(e=='不限'){
  this.minRent=1
  this.maxRent=10000
}
  console.log('rent'+this.minRent);
  console.log('rent'+this.maxRent);
this.invokeListHouse(this._selectedProvince,this.selectedCity,this.selectedRegion,this.minSize,this.maxSize,this.minRent,this.maxRent,this.order,this.type,this.SearchMsg,this.PageIndex,this.PageSize)
  
  }
siftOrder(e:any){
  this.oderTure=e
    if(e=='升序'){
      // console.log('oder:'+1);
      this.order=1
    }else{
      // console.log('oder:'+2);
      this.order=2
    }
this.invokeListHouse(this._selectedProvince,this.selectedCity,this.selectedRegion,this.minSize,this.maxSize,this.minRent,this.maxRent,this.order,this.type,this.SearchMsg,this.PageIndex,this.PageSize)
}
  
// 页码index
PageIndex:number=1
// 每页几条数据
PageSize:number=8
// 总数
Total:number=1
// 页码改变时回调
PageIndexChange(e:any){
  console.log('页码改变');
  console.log("searech:"+this.SearchMsg);
  console.log("order"+this.order);
  
  console.log('index:'+e);
  // console.log('SearchMsg:'+this.SearchMsg);
  // console.log('Total:'+this.Total);
  this.PageIndex=e
  console.log('PageSize:'+this.PageSize);
  this.invokeListHouse(this._selectedProvince,this.selectedCity,this.selectedRegion,this.minSize,this.maxSize,this.minRent,this.maxRent,this.order,this.type,this.SearchMsg,e,this.PageSize)
}

// 

}





